import HttpClient from "./net/HttpClient";

let api;
if (location.origin.split('pbani').length > 1) {
    api = `http://weixin.pbani.com/wx/`;
} else {
    api = `http://127.0.0.1/wx/`;
}


export function getUrlParameter(sParam) {
    let sPageURL = decodeURIComponent(window.location.search.substring(1)),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : sParameterName[1];
        }
    }
}

export function change_url(url) {
    window.location.href = url
}

export function getCookie(name) {
    let value = "; " + document.cookie;
    let parts = value.split("; " + name + "=");
    if (parts.length === 2) return parts.pop().split(";").shift();
}

export function http(arr) {
    if (typeof arr === 'object') {
        arr = [arr]
    }

    console.log(arr);

    for (let i in arr) {
        if (arr.hasOwnProperty(i)) {
            arr[i].url = `${api}${arr[i].url}?token=${token}`;
        }
    }

    console.log(arr);

    loadingToast();
    let res = HttpClient(arr);
    setTimeout(()=>{
        let loadingToast = $('#loadingToast');
        if (loadingToast.css('display') !== 'none') {
            loadingToast.fadeOut('fast');
        }
    },500);
    return res
}

export function send(cmd, data = {}, func = function () {
}) {
    return new Promise((resolve, reject) => {
        data['token'] = token;
        // alert(JSON.stringify(data))
        window.socket.emit(cmd, data, function () {
            let data = func();
            if (!data) {
                data = 'hello'
            }
            resolve(data);
        });
    });
}

export function htmlDecodeByRegExp(str) {
    let s;
    if (str.length === 0) return "";
    s = str.replace(/&amp;/g, "&");
    s = s.replace(/&lt;/g, "<");
    s = s.replace(/&gt;/g, ">");
    s = s.replace(/&nbsp;/g, " ");
    s = s.replace(/&#39;/g, "\"");
    s = s.replace(/&quot;/g, "\"");
    return s;
}

Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(),    //day
        "h+": this.getHours(),   //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
        "S": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
        (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
            RegExp.$1.length == 1 ? o[k] :
                ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}

function loadingToast() {
    let msg;
    arguments[0] ? msg = arguments[0] : msg = '数据加载中';
    let loadingToast;
    loadingToast = '\
           <div id="loadingToast" class="weui_loading_toast pos_a w100 h100 t0" style="display:none;">\
             <div class="weui_mask_transparent"></div>\
             <div class="weui_toast">\
                 <div class="weui_loading">\
                     <div class="weui_loading_leaf weui_loading_leaf_0"></div>\
                     <div class="weui_loading_leaf weui_loading_leaf_1"></div>\
                     <div class="weui_loading_leaf weui_loading_leaf_2"></div>\
                     <div class="weui_loading_leaf weui_loading_leaf_3"></div>\
                     <div class="weui_loading_leaf weui_loading_leaf_4"></div>\
                     <div class="weui_loading_leaf weui_loading_leaf_5"></div>\
                     <div class="weui_loading_leaf weui_loading_leaf_6"></div>\
                     <div class="weui_loading_leaf weui_loading_leaf_7"></div>\
                     <div class="weui_loading_leaf weui_loading_leaf_8"></div>\
                     <div class="weui_loading_leaf weui_loading_leaf_9"></div>\
                     <div class="weui_loading_leaf weui_loading_leaf_10"></div>\
                     <div class="weui_loading_leaf weui_loading_leaf_11"></div>\
                 </div>\
                 <p class="weui_toast_content">' + msg + '</p>\
              </div>\
           </div>\
        ';
    if (!$('#loadingToast').length) {
        $('body').append(loadingToast);
    } else {
        $('#loadingToast.weui_toast_content').html(msg);
    }
    $('#loadingToast').fadeIn('fast');
}
